package com.aynu.webshop.web.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aynu.webshop.web.model.Cart;
import com.aynu.webshop.web.model.Goods;
import com.aynu.webshop.web.model.User;
import com.aynu.webshop.web.service.CartService;
import com.aynu.webshop.web.service.GoodsService;
import com.aynu.webshop.web.service.UserService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping(value = "/cart")
public class CartController {
	@Resource
	private CartService cartService;
	@Resource
	private UserService userService;
	@Resource
	private GoodsService goodsService;
	
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：我的购物车
	 */
	@RequestMapping(value = "/myCart", method = RequestMethod.GET)
	public ModelAndView myCart(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		ModelAndView mv = new ModelAndView();
		List<Cart> carList=cartService.selectByuId(user.getuId());
		mv.addObject("carList", carList);
		mv.setViewName("cart/myCart");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：添加商品至购物车
	 */
	@RequestMapping(value = "/addToCart", method = RequestMethod.GET)
	public ModelAndView addToCart(HttpServletRequest request,Integer gid,Integer carCount ){
		System.out.println(carCount);
		ModelAndView mv=null;
		User user = (User) request.getSession().getAttribute("user");
		if(user!=null){
			System.out.println("进入判断用户是否为空的方法了！");
			List<Cart> cartList = cartService.selectByuId(user.getuId());
			mv = new ModelAndView("redirect:myCart");
			//categoryService.insert(category);
			Cart car = new Cart();
			if(cartList.size()!=0){
				System.out.println("进入判断购物车列表是否为空的方法了！"+cartList);
				for(int i =0;i<cartList.size();i++){
					if(cartList.get(i).getgId()==gid){
						System.out.println("进入判断购物车是否有该商品的方法了！");
						car = cartService.selectById(cartList.get(i).getCarId());
						car.setCarCount(car.getCarCount()+1);
						cartService.update(car);
					}else{
						System.out.println("没有该商品添加商品至购物车！");
						car.setgId(gid);
						car.setCarCount(carCount);
						car.setuId(user.getuId());
						car.setUser(userService.selectById(user.getuId()));
						car.setGoods(goodsService.selectById(gid));
						System.out.println(car);
						cartService.insert(car);
					}
				}
			}else{
				System.out.println("进入购物车为空添加商品至购物车的方法了！");
				car.setgId(gid);
				car.setCarCount(carCount);
				car.setuId(user.getuId());
				car.setUser(userService.selectById(user.getuId()));
				car.setGoods(goodsService.selectById(gid));
				System.out.println(car);
				cartService.insert(car);
			}
		//	mv.setViewName("cart/myCart");
			return mv;
		}else{
			mv = new ModelAndView();
			mv.setViewName("../../login");
			return mv;
		}
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除购物车
	 */
	@RequestMapping(value = "/deleteThis", method = RequestMethod.GET)
	public ModelAndView deleteThis(HttpServletRequest request,Integer carid){
		ModelAndView mv = new ModelAndView("redirect:myCart");
		cartService.delete(carid);
		return mv;
	}
	
	@RequestMapping(value = "/UpdateCount", method = RequestMethod.POST)
	public String getAllCategoryOne(HttpServletRequest request, HttpServletResponse response ,Integer cartId,Integer count) {
		JSONObject result = new JSONObject();
		Cart cart = cartService.selectById(cartId);
		cart.setCarCount(count);
		cartService.update(cart);
		Goods goods = goodsService.selectById(cart.getgId());
		Double price=count*goods.getgPrice();
		result.put("price", price);
		result.put("count", count);
		response.setCharacterEncoding("UTF-8");  
        try {  
            response.getWriter().write(result.toString());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
		return null;
	}
}
